package com.whosly.fast.web.quickly.command;

import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
@ConditionalOnProperty(prefix="spring.data.redis", name = "service")
public class CheckRedisCommandLineRunner implements InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(CheckRedisCommandLineRunner.class);

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void afterPropertiesSet() throws Exception {
        if(redisTemplate == null) {
            logger.error("redisTemplate is null!");
            return;
        }

        // 设置键值对
        redisTemplate.opsForValue().set("key", "value");
        // 获取值
        String value = (String) redisTemplate.opsForValue().get("key");
        Assert.isTrue(StringUtils.equals(value, "value"), "not match");
    }
}
